package com.gxa.insurance.service.impl;

import com.gxa.insurance.exception.BusinessException;
import com.gxa.insurance.exception.ExceptionCode;
import com.gxa.insurance.mapper.*;
import com.gxa.insurance.pojo.*;
import com.gxa.insurance.service.NewCarOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/**
 * @author wang li sha
 * @date 2022-06-06 16:33
 * <p>
 * <p>
 * 点击保存：
 * 修改总订单：
 * 1.根据订单编号查询订单Order对象order
 * 2.获取前端传入的map中的订单的各个字段，并修改order的字段（派工单位、订单状态、车牌号）
 * 3.定义update方法，传入参数order。根据order.id修改order表中记录的各字段
 * 修改新车上牌订单详情：
 * 1. 获取order_id
 * 2. 通过order_id查询tb_new_car_order表中的NewCarOrder对象newCarOrder
 * 3. 获取前端传入的map中的订单的各个字段，并修改newCarOrder的除id和order_id的字段
 * 4. 定义update方法，传入参数newCarOrder。根据id修改tb_new_car_order表中的各字段
 */
@Service("NewCarOrderService")
@Transactional(rollbackFor = Throwable.class)
public class NewCarOrderServiceImpl implements NewCarOrderService {
    @Autowired
    NewCarOrderMapper newCarOrderMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    AreaMapper areaMapper;
    @Autowired
    DriverMapper driverMapper;
    @Autowired
    RepairDepotMapper repairDepotMapper;

    @Override
    public Integer update(Map<String, String> maps) throws Exception {
        Order order = orderMapper.selectOrderByNumber(maps.get("orderNumber"));
        if (order == null) {
            throw new BusinessException(ExceptionCode.ORDER_NOT_FONUD);
        }
        order.setWorkCompany(maps.get("workCompany"));

        OrderStatus status = orderMapper.selectOrderStatus(maps.get("orderStatus"));
        order.setStatus(status);


        order.setCarId(maps.get("carId"));
        Integer updateOrder = orderMapper.updateNewCarOrderTotalOrder(order);
        if (updateOrder != 1) {
            throw new BusinessException(ExceptionCode.UPDATE_ORDER_FAILED);
        }
        Integer orderId = order.getId();
        NewCarOrder newCarOrder = newCarOrderMapper.selectNewCarOrderByOrderId(orderId);
        if (newCarOrder == null) {
            throw new BusinessException(ExceptionCode.CARORDER_NOT_FONUD);
        }

        Area area = areaMapper.selectAreaByName(maps.get("s4Area"));
        newCarOrder.setArea(area);
        
        Driver driver = driverMapper.selectDriverByName(maps.get("driver"));
        newCarOrder.setDriver(driver);

        newCarOrder.setDriverArea(maps.get("driverArea"));

        newCarOrder.setIsMakeCompemsation(Boolean.parseBoolean(maps.get("isMakeCompemsation")));
        newCarOrder.setIsSendMessage(Boolean.parseBoolean(maps.get("isSendMessage")));
        newCarOrder.setAttention(maps.get("attention"));
        newCarOrder.setUserMessageContent(maps.get("userMessageContent"));
        newCarOrder.setDriverMessageContent(maps.get("driverMessageContent"));
        newCarOrder.setContactPerson(maps.get("contactPerson"));
        newCarOrder.setContactPhone(maps.get("contactPhone"));
        newCarOrder.setInspectPlaceMap(maps.get("inspectPlaceMap"));

        RepairDepot repairDepot = repairDepotMapper.selectByName(maps.get("inspectPlace"));
        newCarOrder.setInspectPlace(repairDepot);

        newCarOrder.setTip(maps.get("tip"));
        newCarOrder.setCreateTime(new Date(System.currentTimeMillis()));
        Integer update = newCarOrderMapper.update(newCarOrder);
        if (update != 1) {
            throw new BusinessException(ExceptionCode.UPDATE_CARORDER_FAILED);
        }
        return update;
    }
}
